package org.coding.springbootdemocommandline.fileopr.util;

/**
 * ^ enjoy coding and be bugfree boy
 *
 * @author guoxh
 * @date 2020/11/7 4:29 下午
 * @desc
 **/
public class ExcelUtils {
    public static String excelColIndexToStr(int columnIndex) {
        if (columnIndex <= 0) {
            return null;
        }
        String columnStr = "";
        columnIndex--;
        do {
            if (columnStr.length() > 0) {
                columnIndex--;
            }
            columnStr = ((char) (columnIndex % 26 + (int) 'A')) + columnStr;
            columnIndex = (int) ((columnIndex - columnIndex % 26) / 26);
        } while (columnIndex > 0);
        return columnStr;
    }

    /**
     * 将excel中的A,B,C,D 转化为0,1,2,3
     *
     * @param col
     * @return
     */
    public static int getExcelCol(String col) {
        col = col.toUpperCase();
        //从-1开始计算,字母从1开始运算.这种总数下来算数正好相同
        int count = 0;
        char[] cs = col.toCharArray();
        for (int i = 0; i < cs.length; i++) {
            count += (cs[i] - 64) * Math.pow(26, cs.length - 1 - i);
        }
        return count;
    }
}
